package com.lijj.activiti.demo.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.Execution;

public class ElementInstance {
	@org.junit.Test
	public void test() {
		ProcessEngine engine = ProcessEngineConfiguration
				.createProcessEngineConfigurationFromResource(
						"temp/activiti.cfg.xml").buildProcessEngine();
		RepositoryService repositoryService = engine.getRepositoryService();
		RuntimeService runtimeService = engine.getRuntimeService();
		Deployment deployment = repositoryService.createDeployment()
				.addClasspathResource("bpmn/ElementInstance.bpmn").deploy();
		Deployment depA = repositoryService.createDeployment()
				.addString("a1", "a1").addString("a2", "a2")
				.addString("a3", "a3").name("a").deploy();
		Deployment depB = repositoryService.createDeployment()
				.addString("b1", "b1").addString("b2", "b2")
				.addString("b3", "b3").name("b").deploy();
		Deployment depC = repositoryService.createDeployment()
				.addString("c1", "c1").addString("c2", "c2")
				.addString("c3", "c3").name("c").deploy();
		Deployment depD = repositoryService.createDeployment()
				.addString("d1", "d1").addString("d2", "d2")
				.addString("d3", "d3").name("d").deploy();
		Deployment depE = repositoryService.createDeployment()
				.addString("e1", "e1").addString("e2", "e2")
				.addString("e3", "e3").name("e").deploy();
		Deployment depAQuery=repositoryService.createDeploymentQuery().deploymentId(depA.getId()).singleResult();
		System.out.println("根据id查询："+depAQuery.getName());
		Deployment depBQuery=repositoryService.createDeploymentQuery().deploymentName("b").singleResult();
		System.out.println("查询名称为b："+depBQuery.getName());
		List<Deployment> depCQuery=repositoryService.createDeploymentQuery().deploymentNameLike("%b%").list();
		System.out.println("模糊查询b，结果数量为："+depCQuery.size());
		
		// ProcessDefinition definition = repositoryService
		// .createProcessDefinitionQuery()
		// .deploymentId(deployment.getId()).singleResult();
		// List<String>
		// nameStrings=repositoryService.getDeploymentResourceNames(deployment.getId());
		// for (String string : nameStrings) {
		// System.out.println(string);
		// }
		// InputStream
		// is=repositoryService.getProcessDiagram(definition.getId());
		// try {
		// BufferedImage image=ImageIO.read(is);
		// File file=new File("d:/ElementInstance.png");
		// if(!file.exists()){
		// file.createNewFile();
		// }
		// FileOutputStream osFileOutputStream=new FileOutputStream(file);
		// ImageIO.write(image, "png", osFileOutputStream);
		// osFileOutputStream.close();
		// is.close();
		// } catch (IOException e) {
		// e.printStackTrace();
		// }

		// InputStream is=repositoryService.getProcessModel(definition.getId());
		// InputStream
		// is=repositoryService.getResourceAsStream(deployment.getId(),"bpmn/ElementInstance.bpmn");
		// try {
		// int count=is.available();
		// byte[] contents=new byte[count];
		// is.read(contents);
		// String result=new String(contents);
		// System.out.println(result);
		// } catch (IOException e) {
		// e.printStackTrace();
		// }

		List<String> datas = new ArrayList<String>();
		Map<String, Object> vars = new HashMap<String, Object>();
		datas.add("a");
		datas.add("b");
		datas.add("c");
		datas.add("d");
		vars.put("datas", datas);
		// IdentityService identityService=engine.getIdentityService();
		// identityService.createGroupQuery().potentialStarter("").list();
		// repositoryService.getIdentityLinksForProcessDefinition("");
		// InputStream is=repositoryService.getResourceAsStream("","1");

		runtimeService.startProcessInstanceByKey("elementInstance", vars);
		Execution execution = runtimeService.createExecutionQuery()
				.singleResult();
		System.out.println("主执行流id：" + execution.getId());
	}
}
